(function($){
  "use strict";
  $.fn.leftSlide = function(option){
    var self = $(this);
    var defaults = {
      max: 100    // 左滑的最大值
    };
    var params = $.extend({},defaults,option);
    var isShow = null;
    var this_x, this_y, target_x, target_y, move_x, move_y, slideX, slideY, objX;
    /* this_ 触摸位置
       target_ 滑动时的位置
       move_ 移动距离
       objX 目标对象位置 */
    self.on("touchstart", function (e) {
      this_x = e.originalEvent.targetTouches[0].pageX;
      this_y = e.originalEvent.targetTouches[0].pageY;
      slideX = true;
      slideY = true;
      objX = ($(this).css('transform').replace(/[^0-9\-,]/g, '').split(','))[4];
      if (!$(this).hasClass("showing")) {
        if (isShow) {
          isShow.css("transform", "translateX(0px)");
        }
      }
    });
    self.on("touchmove", function (e) {
      target_x = e.originalEvent.targetTouches[0].pageX;
      target_y = e.originalEvent.targetTouches[0].pageY;
      move_x = target_x - this_x;
      move_y = target_y - this_y;
      // 左右移动
      if (slideX && Math.abs(move_x) - Math.abs(move_y) > 0) {
        e.stopPropagation();
        if (objX == 0) {
          if (move_x < 0) {
            e.preventDefault();
            var l = Math.abs(move_x);
            if (Math.abs(move_x) > params.max) {
              l = params.max;
            } else {
              l = Math.abs(move_x);
            }
            $(this).css({
              "transform": "translateX(" + -l + "px)"
            });
          } else if (move_x >= 0) {
            e.preventDefault();
            $(this).css("transform", "translateX(0px)");
          }
        } else if (objX < 0) {
          if (move_x >= 0) {
            e.preventDefault();
            var r = -params.max + Math.abs(move_x);
            if (r > 0) {
              r = 0;
            } else {
              r = -params.max + Math.abs(move_x);
            }
            $(this).css({
              "transform": "translateX(" + r + "px)"
            });
          } else {
            e.preventDefault();
            $(this).css("transform", "translateX(" + -params.max + "px)");
          }
        }
        slideY = false;
      }
      // 上下滑动
      if (slideY && Math.abs(move_x) - Math.abs(move_y) < 0) {
        slideX = false;
      }
    });
    self.on("touchend", function () {
      objX = ($(this).css('transform').replace(/[^0-9\-,]/g, '').split(','))[4];
      if (objX > -(params.max / 3)) {
        $(this).css("transform", "translateX(0px)");
        self.removeClass("showing");
        objX = 0;
      } else {
        $(this).css("transform", "translateX(" + -params.max + "px)");
        self.removeClass("showing");
        $(this).addClass("showing");
        isShow = $(this);
        objX = -params.max;
      }
    });
  };
})($);